//
//  YNSPayVC.m
//  HNBaseProject
//
//  Created by TR on 2024/7/24.
//

#import "YNSPayVC.h"
#import "YNSPayCCell.h"
#import "YNSPayReusableHeadView.h"
#import "YNSPayReusableFootView.h"
#import "YRStoreManager.h"
#import <AppsFlyerLib/AppsFlyerLib.h>
@interface YNSPayVC ()<UICollectionViewDelegate,UICollectionViewDataSource,MxNavigationViewDelegate>

@property(nonatomic,strong)MxPublicNav*nav;
@property(nonatomic,strong) UICollectionView *collectV;
@property(nonatomic,strong) NSArray *listArray;
@property(nonatomic,assign) NSInteger selectInx;
@property(nonatomic,copy) NSString *diamond_set_id;
@end

@implementation YNSPayVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.diamond_set_id = @"";
    self.selectInx = -1;
    self.listArray = @[
        @{
            @"count" : @"30",
            @"money" : @"4.99",
        },
        @{
            @"count" : @"48",
            @"money" : @"6.99",
        },
        @{
            @"count" : @"92",
            @"money" : @"12.99",
        },
        @{
            @"count" : @"485",
            @"money" : @"68",
        },
        @{
            @"count" : @"2384",
            @"money" : @"329",
        },
        @{
            @"count" : @"4772",
            @"money" : @"649",
        },
    ];
    [self setUI];

}


-(void)setUI{
    self.nav = [[MxPublicNav alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kNavBarHeight)];
    self.nav.titleLab.text = @"充值";
    self.nav.delegate = self;
    self.nav.line.hidden = NO;
    [self.view addSubview:self.nav];
    
    UICollectionViewFlowLayout *lay = [[UICollectionViewFlowLayout alloc]init];
    lay.itemSize = CGSizeMake((kScreenW-50)/3, 148);
    lay.minimumLineSpacing = 10;
    lay.minimumInteritemSpacing = 10;
    lay.sectionInset = UIEdgeInsetsMake(0, 15, 0, 15);
    self.collectV = [[UICollectionView alloc]initWithFrame:CGRectMake(0, kNavBarHeight, kScreenW, kScreenH-kNavBarHeight) collectionViewLayout:lay];
    self.collectV.delegate = self;
    self.collectV.dataSource = self;
    self.collectV.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectV];
    [self.collectV registerClass:[YNSPayCCell class] forCellWithReuseIdentifier:@"YNSPayCCell"];
    [self.collectV registerClass:[YNSPayReusableHeadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"YNSPayReusableHeadView"];
    [self.collectV registerClass:[YNSPayReusableFootView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"YNSPayReusableFootView"];
}
#pragma mark ---MxNavigationViewDelegate-----
- (void)leftTap {
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark----代理方法------
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.listArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dict = self.listArray[indexPath.item];
    YNSPayCCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YNSPayCCell" forIndexPath:indexPath];
    [cell reloadCellWithDict:dict select:(self.selectInx == indexPath.item)];
    return cell;
}
- (CGSize)collectionView:(UICollectionView*)collectionView layout:(nonnull UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(kScreenW, 104+kRealValue(122));
}
- (CGSize)collectionView:(UICollectionView*)collectionView layout:(nonnull UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    return CGSizeMake(kScreenW, 220);
}
- (UICollectionReusableView*)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    WEAKSELF;
    if (kind == UICollectionElementKindSectionHeader)
    {
        YNSPayReusableHeadView*headview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"YNSPayReusableHeadView" forIndexPath:indexPath];
        return headview;
    }
    else{
        YNSPayReusableFootView*footView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"YNSPayReusableFootView" forIndexPath:indexPath];
        [footView.payButton addTarget:self action:@selector(surePay) forControlEvents:UIControlEventTouchUpInside];
        return footView;
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    self.selectInx = indexPath.item;
    self.diamond_set_id = [NSString stringWithFormat:@"aivideo_recharge_0%ld",indexPath.item+1];
    [self.collectV reloadData];
}

- (void)surePay {
    if (self.selectInx == -1) {
        [self.view xjmakeToast:@"pelease select diamond"];
        return;
    }
    //统计
    NSDictionary *dic = self.listArray[self.selectInx];
    NSString *moenyStr = [NSString stringWithFormat:@"%@",dic[@"money"]];
    NSString *numStr = [NSString stringWithFormat:@"%@",dic[@"count"]];
    [[AppsFlyerLib shared]  logEvent: AFEventContentView withValues: @{
        AFEventParamPrice: moenyStr,
        AFEventParamQuantity:numStr,
        AFEventParamContentId: @"3",
        AFEventParamContentType:@"制作视频"
    }];
    MJWeakSelf
    [[UIApplication sharedApplication].keyWindow makeToastActivity];
    [[DCHttpClient shareClient] requestWithPath:@"api/diamond.diamond/diamondOrderSn" params:@{} httpMethod:DCHttpRequestGet sucess:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        NSDictionary * dict = [responseObject mj_JSONObject];
        if([dict[@"code"] intValue]==1){
            NSString *order_sn = dict[@"data"][@"order_sn"];//订单号
            
            [[YRStoreManager shareManager] startPurchaseWithID:self.diamond_set_id orderId:order_sn isRechange:YES completeHandle:^(IAPPurchType type, NSString * _Nonnull message) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [[UIApplication sharedApplication].keyWindow hideToastActivity];
                    if (type == IAPPurchSuccess) {
                        [weakSelf.view xjmakeToast:@"购买成功"];
                        [[NSNotificationCenter defaultCenter] postNotificationName:@"buysuccess" object:nil];
                    } else {
                        if (type == IAPPurchCancel) {
                            [weakSelf.view xjmakeToast:message];
                        } else {
                            [weakSelf.view xjmakeToast:message];
                        }
                    }
                });
            }];
        } else {
            [[UIApplication sharedApplication].keyWindow hideToastActivity];
        }
    } failture:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        [weakSelf.view hideToastActivity];
    }];
}



@end
